<?php
/**
 * SiteController class file.
 *
 * @author Zhang Jinlong <466028373@qq.com>
 * @link http://www.ijianzhi.cn
 * @copyright Copyright &copy; 2013 DianJin Soft
 */

class SiteController extends Controller
{
    /**
     * 首页
     */
	public function actionIndex()
	{
	    $this->render('index');
	}

	/**
	 * 登录页面
	 */
	public function actionLogin()
	{
	    $this->render('login');
	}

	/**
	 * 退出登录
	 */
	public function actionLogout()
	{
	    Yii::app()->user->logout();

	    $this->redirect(array('login'));
	}

	public function actionImport()
	{
	    $pattern = "/'code':'(\d+)','name':'(.*?)'/";

        $cityUrl = 'http://my.lezhi.com/PhpDataBridge/databridge.php?url=qid%3D2200%26datatype%3Djs%26pcode%3D460000000000000000000000000000000000%26buff%3D86400';
        $cityData = WebHelper::get($cityUrl);

        preg_match_all($pattern, $cityData, $cityMatches, PREG_SET_ORDER);

        foreach ($cityMatches as $city)
        {
            $addressModel = new SysAddressModel();
            $addressModel->code = substr($city[1], 0, 8);
            $addressModel->title = $city[2];
            $addressModel->pinyin = Pinyin($city[2], 'utf-8');
            $addressModel->save(FALSE);

            $countyUrl = 'http://my.lezhi.com/PhpDataBridge/databridge.php?url=qid%3D2300%26datatype%3Djs%26tcode%3D'.$city[1].'%26buff%3D86400';
            $countyData = WebHelper::get($countyUrl);

            preg_match_all($pattern, $countyData, $countyMatches, PREG_SET_ORDER);

            foreach ($countyMatches as $county)
            {
                if ($county[2] === '三亚市')
                {
                    continue;
                }

                $addressModel = new SysAddressModel();
                $addressModel->code = substr($county[1], 0, 8);
                $addressModel->title = $county[2];
                $addressModel->pinyin = Pinyin($county[2], 'utf-8');
                $addressModel->save(FALSE);
            }
        }
	}

	/**
	 * 错误处理
	 */
	public function actionError()
	{
	    if (($error = Yii::app()->errorHandler->error) !== NULL)
	    {
	    	if (Yii::app()->request->isAjaxRequest)
	    	{
	    	    echo $error['message'];
	    	}
	    	else
	    	{
	    	    $this->renderPartial('error', $error);
	    	}
	    }
	}

	public function actions()
	{
	    return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
	    );
	}
}